#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const ll mod = 1e9 + 7;
const ull bs1 = 31;
const ull bs2 = 9971;

struct node {
	ull x, y;
} d[10001000];

ll n, t, ans;
ll a[1100];
ll b[1100];
ll s[1100];
ll f[1100][1100];
ll g[100100];
bool sub1, sub2;

void dfs(ll x, ll y) {
	if (x == n && y == n) {
		t++;
		d[t].x = d[t].y = 0;
		for (ll i = 1; i <= 2 * n - 2; i++) {
			d[t].x = d[t].x * bs1 + s[i];
			d[t].y = d[t].y * bs2 + s[i];
		}
		return;
	} else {
		if (x != n) {
			s[x + y - 1] = b[y];
			dfs(x + 1, y);
		}
		if (y != n) {
			s[x + y - 1] = a[x];
			dfs(x, y + 1);
		}
	}
	return;
}

bool cmp(node xx, node yy) {
	return xx.x < yy.x || xx.x == yy.x && xx.y < yy.y;
}

int main() {
	freopen("zha.in", "r", stdin);
	freopen("zha.out", "w", stdout);
	scanf("%lld", &n);
	for (ll i = 1; i <= n; i++) {
		scanf("%lld", &a[i]);
	}
	for (ll i = 1; i <= n; i++) {
		scanf("%lld", &b[i]);
	}
	if (n <= 10) {
		dfs(1, 1);
		ans = t;
		sort(d + 1, d + t + 1, cmp);
		for (ll i = 2; i <= t; i++) {
			if (d[i].x == d[i - 1].x && d[i].y == d[i - 1].y) {
				ans--;
			}
		}
		printf("%lld\n", ans);
		return 0;
	}
	sub1 = 1;
	sub2 = 1;
	for (ll i = 1; i <= n; i++) {
		if (a[i] == b[i]) {
			sub1 = 0;
		} else {
			sub2 = 0;
		}
	}
	if (sub1) {
		f[1][1] = 1;
		for (ll i = 1; i < 2 * n; i++) {
			for (ll j = 1; j <= n; j++) {
				ll k = i - j;
				if (k < 1 || k > n) {
					continue;
				}
				if (j != n) {
					f[j + 1][k] = (f[j + 1][k] + f[j][k]) % mod;
				}
				if (k != n) {
					f[j][k + 1] = (f[j][k + 1] + f[j][k]) % mod;
				}
				if (j != n && k != n && a[j] == b[k]) {
					f[j + 1][k + 1] = (f[j + 1][k + 1] - f[j][k] + mod) % mod;
				}
			}
		}
		printf("%lld\n", f[n][n]);
		return 0;
	}
	if (sub2) {
		f[1][1] = 1;
		for (ll i = 1; i <= n; i++) {
			f[i][1] = 1;
			for (ll j = 2; j <= i; j++) {
				f[i][j] = (f[i][j - 1] + f[i - 1][j]) % mod;
			}
		}
		printf("%lld\n", f[n][n]);
		return 0;
	}
	f[1][1] = 1;
	for (ll i = 1; i < 2 * n; i++) {
		for (ll j = 1; j <= n; j++) {
			ll k = i - j;
			if (k < 1 || k > n) {
				continue;
			}
			if (j != n) {
				f[j + 1][k] = (f[j + 1][k] + f[j][k]) % mod;
			}
			if (k != n) {
				f[j][k + 1] = (f[j][k + 1] + f[j][k]) % mod;
			}
			for (ll l = 2; l <= n; l++) {
				if (k + l - 1 <= n && j + l - 1 <= n) {
					if (a[j + l - 1] == b[k + l - 1]) {
						f[j + l - 1][k + l - 1] = (f[j + l - 1][k + l - 1] - x * f[j][k] + mod) % mod;
						break;
					}
				}
			}
		}
	}
	printf("%lld\n", f[n][n]);
	return 0;
}
